package com.gxan.web.servlet.recruitServlet;

import com.alibaba.fastjson.JSONObject;
import com.gxan.pojo.Recruit;
import com.gxan.service.RecruitService;
import com.gxan.service.impl.RecruitServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/recruit/jobPost/addPost")
public class AddPostServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=utf-8");

        Map resultMap = new HashMap<>();
        Date regist_time;
        Date deadline;

        try {
            String params = req.getReader().readLine();
            Recruit recruit = JSONObject.parseObject(params,Recruit.class);

            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            String regTime = sdf.format(recruit.getRegist_time());
            String endTime = sdf.format(recruit.getDeadline());
            regist_time = sdf.parse(regTime);
            deadline = sdf.parse(endTime);
            recruit.setRegist_time(regist_time);
            recruit.setDeadline(deadline);

            RecruitService recruitService = new RecruitServiceImpl();
            int res = recruitService.addPost(recruit);
            if(res > 0){
                resultMap.put("code",0);
                resultMap.put("msg","添加成功！");
                // 需要将Java对象转换为json字符串对象才能在网络中进行传输
                resp.getWriter().write(JSONObject.toJSONString(resultMap));
            }
        } catch (Exception e){
            e.printStackTrace();
            resultMap.put("code",-1);
            resultMap.put("msg","系统开小差了，请联系管理员");
            // 需要将Java对象转换为json字符串对象才能在网络中进行传输
            resp.getWriter().write(JSONObject.toJSONString(resultMap));
        }
        resp.getWriter().flush();
        resp.getWriter().close();

    }
}
